/**
 * TODO
 */
package com.bci.pwtz.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.DydlzdMapper;
import com.bci.pwtz.mysql.model.Dydlzd;
import com.bci.pwtz.service.BusinessRefService;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.DydlzdService;

/**
 * TODO
 * 
 * @author ICLNetwork Team
 * @version 3.0
 */
@Service
public class DydlzdServiceImpl implements DydlzdService
{
    private DydlzdMapper            objMapper;

    private CompanyService          companyService;

    private BusinessRefService businessRefService;

    private static final Logger     _logger = Logger.getLogger(PdbyqServiceImpl.class);

    public DydlzdMapper getObjMapper()
    {
        return objMapper;
    }

    public void setObjMapper(DydlzdMapper objMapper)
    {
        this.objMapper = objMapper;
    }

    public CompanyService getCompanyService()
    {
        return companyService;
    }

    public void setCompanyService(CompanyService companyService)
    {
        this.companyService = companyService;
    }

  
    public BusinessRefService getBusinessRefService()
    {
        return businessRefService;
    }

    public void setBusinessRefService(BusinessRefService businessRefService)
    {
        this.businessRefService = businessRefService;
    }

    /**
     * 新增低压电缆终端信息
     */
    @Override
    public Dydlzd add(Dydlzd dydlzd) throws PwtzDatabaseException
    {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("add:dydlzd=" + dydlzd.toString());
        }
        if (dydlzd == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("add,dydlzd is null");
            }
            return null;
        }
        else
        {
           /* UUID uuid = UUID.randomUUID();
            dydlzd.setDeviceid(uuid.toString());*/
            dydlzd.setStatus(1);
            dydlzd.setCreatedate(new Date());
            dydlzd.setLastmodifiedtime(new Date());
        }
        try
        {
            objMapper.insertSelective(dydlzd);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return dydlzd;
    }

    /**
     * 删除低压电缆终端
     */
    @Override
    public int delete(String dydlzdId) throws PwtzDatabaseException, CanNotDeleteException
    {
        int result = 0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("delete:dydlzdId=" + dydlzdId);
        }
        try
        {
            boolean bl = businessRefService.canDelete("T_DYDLZD", dydlzdId);
            if (!bl)
            {
                throw new CanNotDeleteException();
            }
            result = objMapper.deleteById(dydlzdId,new Date());
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    /**
     * 修改低压电缆终端信息
     */
    @Override
    public int update(Dydlzd dydlzd) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("update:dydlzd=" + dydlzd.toString());
        }
        if (dydlzd == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("update,dydlzd is null");
            }
            return 0;
        }else{
            dydlzd.setLastmodifiedtime(new Date());
        }
        try
        {
            result = objMapper.updateByPrimaryKeySelective(dydlzd);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    /**
     * 低压电缆终端的load查看方法
     */
    @Override
    public Dydlzd load(String dydlzdId) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("load:dydlzdId=" + dydlzdId);
        }
        Dydlzd dydlzd = null;
        try
        {
            dydlzd = objMapper.selectByPrimaryKey(dydlzdId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (dydlzd != null)
        {
            _logger.debug("load:result dydlzdId=" + dydlzd.toString());
        }
        return dydlzd;
    }

    /**
     * 低压电缆终端的list查询方法
     */
    @Override
    public PaginationList<Dydlzd> findDydlzd(String devicename, Long companyid, int portalType, String[] medLineCodes,String cableCode,String beginTime, String endTime, int pageSize, int pageNo) throws PwtzDatabaseException
    {
        PaginationList<Dydlzd> paginationList = new PaginationList<Dydlzd>();

        if (_logger.isInfoEnabled())
        {
            _logger.info("findDybx:|devicename=" + devicename+"|beginTime=" + beginTime+"|endTime=" + endTime+"|companyid="+companyid+"|portalType="+portalType+"|medLineCodes="+PwtzUtils.parseCharacterArrayToString(medLineCodes)+"|cableCode="+cableCode+"|pageSize="+pageSize+"|pageNo="+pageNo);
        }
        List<Dydlzd> list = null;
        int beginIndex = pageSize * pageNo - pageSize;
        String companyidStr = "";
        String[] companyids = null;
        if (portalType == PwtzConstants.PORTAL_TYPE_WEB)
        {
            companyidStr = companyService.findChildrensId(companyid,null);
            if (companyidStr != null && companyidStr.length() > 0)
            {
                companyidStr = companyidStr.substring(0, companyidStr.length() - 1);
                companyids = companyidStr.split(",");
            }
        }
        try
        {
            list = objMapper.selectDeviceList(devicename, companyid, companyids, portalType,medLineCodes,cableCode, beginTime, endTime, beginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list != null)
        {
            _logger.debug("findDydlzd:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage = 1;
        if (pageSize != 0)
        {
            try
            {
                totalCount = objMapper.selectDeviceCount(devicename, companyid, companyids, portalType,medLineCodes,cableCode, beginTime, endTime);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }
        else
        {
            pageNo = 1;
            if (list != null && list.size() > 0)
            {
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }

    @Override
    public PaginationList<Dydlzd> selectDevice(String devicename, String cablecode, Long companyid, int pagesize, int pageno) throws PwtzDatabaseException
    {
        PaginationList<Dydlzd> paginationList = new PaginationList<Dydlzd>();

        if (_logger.isInfoEnabled())
        {
            _logger.info("findDybx:|devicename=" + devicename+"|companyid="+companyid+"|cableCode="+cablecode+"|pageSize="+pagesize+"|pageNo="+pageno);
        }
        List<Dydlzd> list = null;
        int beginIndex = pagesize * pageno - pagesize;
        try
        {
            list = objMapper.selectDevice(devicename, companyid,cablecode, beginIndex, pagesize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list != null)
        {
            _logger.debug("findDydlzd:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage = 1;
        if (pagesize != 0)
        {
            try
            {
                totalCount = objMapper.selectCount(devicename, companyid,cablecode);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pagesize == 0 ? totalCount / pagesize : totalCount / pagesize + 1;
        }
        else
        {
            pageno = 1;
            if (list != null && list.size() > 0)
            {
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageno);
        paginationList.setPageSize(pagesize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }

}
